<?php
	//----------------------------------------------------------------------------------
	//BACKEND CORE FUNCTIONS
	//This file should not be edited!
	//Write backend functions in "backend.actions.php".
	//----------------------------------------------------------------------------------
	//ACTION FUNCTIONS
	//declared in external file for clarity and to avoid changes to the Backend Core.
	include('backend.actions.php');
	//----------------------------------------------------------------------------------
	
	
	
	
	
	//get response using the actions hub
	$response = actionHub($_GET);
	
	//send response to frontend
	sendResponse($response, 'text');
	
	
	
	
	
	
	function actionHub($request){
		if(!array_key_exists("action", $request)) return getResponse("err", "noAction");
		if(!array_key_exists("data", $request)) return getResponse("err", "noRequestData");
		$action = $request['action'];
		$data 	= $request['data'];
		return call_user_func($action, $data);
	}
	
	function getResponse($type, $description, $data=""){
		$response = array("type" 		=> $type,
						  "description" => $description,
						  "data"		=> $data
						 );
		return $response;
	}
	
	function sendResponse($response, $type='text'){
		if(!is_array($response)) die("responseError");
		if(!array_key_exists("data", $response)) die("noResponseData");
		if(!array_key_exists("data", $response)) die("noResponseData");
		$type = ucfirst($type);
		$typedResponse = call_user_func("send{$type}Response", $response);
		die($typedResponse);
	}
	
	function isError($response){
		if($response['type']=='err') return true;
		return false;
	}
	
	function errorHandler($response){
		if(isError($response)){
			echo print_r($response, true);
			exit;
		}
	}
	
	
	
	
	//----------------------------------------------------------------------------------
	//RESPONSE FUNCTIONS
	function sendTextResponse($response){
		errorHandler($response);
		
		$responseType = $response['type'];
		$responseDesc = $response['description'];
		$responseData = $response['data'];
		$metadata = "$responseType,$responseDesc|";
		
		$output = "";
        if(is_array($responseData)){
            foreach($responseData as $key => $value){
                if($output!="") $output .= ",";
                $output.="$key:$value";
            }
        } else {
            $output = $responseData;
        }

        //set output string and return it
		$output = $metadata . $output;
		return $output;
	}
	
	function sendXmlResponse($data){
	
	}
	
	function sendJsonResponse($data){
	
	}
	//----------------------------------------------------------------------------------
?>